<template>
  <!-- 回收订单  申请售后 -->
  <div>
    <el-dialog :title="title" :visible="dialogVisible" :close-on-click-modal="false" @close="close" width="800px">
      <el-descriptions title="订单信息" :column="3" border :labelStyle="{minWidth:'80px',width:'80px',textAlign:'right'}" :contentStyle="{minWidth:'100px',textAlign:'left'}">
        <el-descriptions-item label="回收编号">{{orderInfo.no}}</el-descriptions-item>
        <el-descriptions-item label="游戏名称">{{orderInfo.gameName}}</el-descriptions-item>
        <el-descriptions-item label="游戏账号" :span="2">{{orderInfo.accountNumber}}</el-descriptions-item>
        <el-descriptions-item label="买家">{{orderInfo.buyerTenantName}}</el-descriptions-item>
        <el-descriptions-item label="卖家" :span="2">{{orderInfo.sellerName}}</el-descriptions-item>
        <el-descriptions-item label="回收价">
          <span style="color:red">¥{{orderInfo.backAmount}}</span>
        </el-descriptions-item>
        <el-descriptions-item label="包赔费" v-if="orderInfo.guaranteeTenantId">
          <span style="color:red">¥{{orderInfo.guaranteeAmount}}</span>
        </el-descriptions-item>
        <el-descriptions-item label="总金额">
          <span style="color:red">¥{{orderInfo.buyerActualAmount}}</span>
        </el-descriptions-item>
      </el-descriptions>
      <div style="height:20px"></div>

      <el-form :model="ruleForm" ref="ruleForm" :inline="true" label-width="120px" :rules="rules" class="demo-ruleForm">
        <el-form-item label="问题类型:" prop="applicantType" >
          <el-select v-model="ruleForm.applicantType" clearable >
            <el-option :label="item.dictLabel" :value="item.dictLabel" v-for="item in ApplicantTypeList" :key="item.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问题描述:" prop="applicantRemark">
          <el-input style="width:500px" type="textarea" v-model="ruleForm.applicantRemark" rows="3" placeholder="请详细描述你的问题..."></el-input>
        </el-form-item>
        <el-form-item label="附件图片">
          <el-upload :action="uploadImgUrl" :file-list="imgFileList" list-type="picture-card" :on-preview="handlePictureCardPreview" :on-exceed="uploadNumber" :multiple="true" :drag="true" :on-success="handleUploadSuccess" :limit="5" :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div class="center">
        <el-button @click="close('ruleForm')">关闭</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')" :loading="loading">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import payApi from "@/api/financeCenter/pay.js";
import onSelfApi from "@/api/retrieveManage/onSelf.js";
export default {
  name: "editPrice",
  data() {
    var priceRule = (rule, value, callback) => {
      if (value === "") {
        callback();
      } else {
        if (!this.priceReg.test(value)) {
          callback(new Error("请输入正确的退款金额"));
        } else if (this.orderInfo.buyerActualAmount < value) {
          callback(new Error("退款金额不能高于买家支付金额！"));
        }
        callback();
      }
    };
    return {
      title: "申请售后",
      dialogVisible: false,
      loading: false,
      uploadImgUrl: "",
      newImages: [],
      imgFileList: [],
      ruleForm: {
        backClueOrderId: "",
        applicantType: "",
        applicantImg: "",
        applicantRemark: "",
      },
      orderInfo: {},
      baopeiZhanghu: {},
      sellerZhanghu: "",
      bankList: [],
      ApplicantTypeList:[],
      rules: {
        applicantType: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
        applicantRemark: [
          {
            required: true,
            message: "请输入问题描述",
            trigger: "blur",
          }
        ],
      },
      userType: "",
    };
  },
  created() {
    this.uploadImgUrl = process.env.VUE_APP_BASE_API + "/upload/image";
  },
  methods: {
    open(order, flag) {
      console.log("%c Line:111 🍌 order", "color:#ffdd4d", order);
      this.loading = false;
      this.orderInfo = order;
      this.newImages = [];
      this.imgFileList = [];
      this.ruleForm.applicantType = "";
      this.ruleForm.applicantRemark = "";
      this.ruleForm.applicantImg = "";
      this.ruleForm.backClueOrderId = this.orderInfo.id;
   
      this.dialogVisible = true;
      this.getApplicantType()
    },
    // 查询售后申请的 问题类型 backOrderAfterApplicantType
    getApplicantType(){
      this.getDicts("backOrderAfterApplicantType").then((res) => {
          if (res.data && res.data.length) {
            this.ApplicantTypeList = res.data
          }
        });
    },
    // 查询收款账户列表
    getBankList(tenantId) {
      console.log("%c Line:181 🍔 tenantId", "color:#b03734", tenantId);
      this.loading = true;
      onSelfApi
        .getBankListByTenantId({
          tenantId: tenantId,
          accountType:1
        })
        .then((response) => {
          this.bankList = response.data;
          this.loading = false;
        });
    },
    // 选择包赔账户
    zhanghuChange(e) {
      if (e && this.bankList.length) {
        this.bankList.forEach((r) => {
          if (r.id == e) {
            // item.collectionAccountName+'('+item.collectionUserName+item.collectionAccount+')'
            this.ruleForm.details = `${r.collectionAccountName} ${r.collectionUserName} ${r.collectionAccount}`;
            //  this.ruleForm.guaranteeTenantCollection = JSON.stringify(r)
          }
        });
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 拼接新上传的图片
          if (this.newImages && this.newImages.length) {
            let newArr = [];
            this.newImages.forEach((img) => {
              newArr.push(img.response.data.filePath);
            });
            this.ruleForm.applicantImg = newArr.join(",");
          } else {
            this.ruleForm.applicantImg = "";
          }
          this.loading = true;
          onSelfApi
            .backClueOrderAfterAdd(this.ruleForm)
            .then((res) => {
              this.dialogVisible = false;
              this.$message.success(res.msg);
              this.$parent.getList();
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 凭证图片上传成功
    handleUploadSuccess(res, file, fileList) {
      if (res.code == 200) {
        // 取得新上传的文件
        this.newImages = fileList.filter((t) => {
          return t.url.indexOf("blob:") > -1;
        });
      }
    },
    // 凭证图片删除
    handleRemove(response, fileList, file) {
      const that = this;
      // 1.获取将要删除图片的临时路径
      const filePath = response.url;
      // 2.找到图片对应的索引值
      this.imgFileList.forEach(function (element, key) {
        if (element.url === filePath) {
          that.imgFileList.splice(key, 1); //回显的图片
        }
      });
      this.newImages.forEach(function (element, key) {
        if (element.url === filePath) {
          that.newImages.splice(key, 1); //新上传的图片
        }
      });
    },
    // 图片预览
    handlePictureCardPreview(file) {
      this.$viewerApi({
        images: [file.url],
      });
    },
    uploadNumber(file, fileList) {
      //限制图片数量
      this.$modal.msgError("最多只能上传5张图片");
    },
    close(formName) {
      this.dialogVisible = false;
    },
  },
};
</script>
<style scoped lang="scss">
::v-deep .el-dialog__body {
  padding-top: 10px !important;
}
::v-deep .el-upload-dragger {
  width: 148px;
  height: 148px;
}

.pick-dialog >>> .el-input--medium input {
  text-align: left;
}
</style>
